//
//  工程名称:XJL_WeChat
//  文件名称:XJLAddViewController.m
//
//  创建者  :Created by 许佳莉
//  创建时间:on 16/8/2.
//  版权   :  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "XJLAddViewController.h"
#import "XJLSearchResultViewController.h"
#import "XJLBuddyQRCodeViewController.h"
#import "MRGQRCodeUtil.h"

@interface XJLAddViewController()<UISearchBarDelegate>
/** 搜索控制器*/
@property(nonatomic,strong) UISearchController *searchController;
/** 搜索结果的控制器*/
@property(nonatomic,strong) XJLSearchResultViewController *searchResultVc;
/** 扫描添加好友*/
@property(nonatomic,strong) XJLBuddyQRCodeViewController *buddyQRCodeVc;

@property (weak, nonatomic) IBOutlet UILabel *myWeChatID;

@end

@implementation XJLAddViewController

-(XJLBuddyQRCodeViewController *)buddyQRCodeVc{
    if (_buddyQRCodeVc == nil) {
        _buddyQRCodeVc = [[XJLBuddyQRCodeViewController alloc]init];
    }
    return _buddyQRCodeVc;
}

-(XJLSearchResultViewController *)searchResultVc{
    if (_searchResultVc == nil) {
        _searchResultVc = [[XJLSearchResultViewController alloc]init];
    }
    return _searchResultVc;
}
-(UISearchController *)searchController{
    if (_searchController == nil) {
        // 不可以使用alloc init 方法来创建搜索控制器
        _searchController = [[UISearchController alloc]initWithSearchResultsController:self.searchResultVc];
        _searchController.searchResultsUpdater = self.searchResultVc;
    }
    return _searchController;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"添加好友";
    
    //1、添加搜索框
    UISearchBar *searchBar = [[UISearchBar alloc]init];
    searchBar.placeholder = @"请输入添加好友的用户名";
    searchBar.delegate = self;
    searchBar.frame = CGRectMake(0, 0, 320, 40);
    
    self.tableView.tableHeaderView = searchBar;
    
    //显示当前用户名
    NSDictionary *loginInfo = [[EaseMob sharedInstance].chatManager loginInfo];
    self.myWeChatID.text = [NSString stringWithFormat:@"我的WeChatID: %@",loginInfo[@"username"]];
}

#pragma mark 搜索框开始编辑的触发方法
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
     //展现搜索控制器
    
    [self presentViewController:self.searchController animated:YES completion:nil];
    return YES;
}

#pragma mark 显示二维码

- (IBAction)showQRCode {
    //调整xib大小
    self.buddyQRCodeVc.view.frame = [UIScreen mainScreen].bounds;
    //显示当前用户的二维码图片
    NSString *loginUserCode = [[EaseMob sharedInstance].chatManager loginInfo][@"username"];
    UIImage *qrCodeImg = [MRGQRCodeUtil genQRCodeImgWithText:loginUserCode];
    self.buddyQRCodeVc.qrCodeImg.image = qrCodeImg;
    [self.view.window addSubview:self.buddyQRCodeVc.view];
}

@end
